home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 May: Tool Chest / Developer CD Series Tool Chest (Apple Computer)(May 1999).iso / Tool Chest / Development Kits / MPW etc / MPW-GM / MPW / Examples / HyperXExamples / AExamples / LittleDialog.a next >
Encoding:
Text File  |  1998-12-03  |  7.9 KB  |  317 lines  |  [TEXT/MPS ]

  1. *    LittleDialog.a
  2. *
  3. *    © 1988,1992 by Apple Computer, Inc.
  4. *    All Rights Reserved
  5. *
  6. *    LittleDialog -- a Hypercard XCMD that displays a modal dialog. This example uses
  7. *    callbacks to HyperCard provided by the HyperXLib.o library, introduced with MPW 3.0.
  8. *    The library provides a consistent set of routines to call HyperCard internal routines
  9. *    from any language. This is the first public demonstration of its use.
  10. *
  11. *    
  12. *    Form: LittleDialog DisplayText
  13. *    
  14. *    Example: LittleDialog "Display this very long text string that wouldn't fit in Answer."
  15. *
  16. *
  17. *    To compile and link this file using MPW assembler, select the following
  18. *    lines (must be uncommented and moved to another worksheet) and press ENTER:
  19. *    
  20. *    make LittleDialog
  21. *    
  22. *    or:
  23. *    
  24. *    Asm LittleDialog.a
  25. *    Link -w -rt XCMD=10002 ∂
  26. *        -m ENTRYPOINT ∂
  27. *        -sg LittleDialog ∂
  28. *        LittleDialog.a.o ∂
  29. *        "{Libraries}HyperXLib.o" ∂
  30. *        "{Libraries}Interface.o" ∂
  31. *        -o "::HyperXExample Stack"
  32. *    Rez LittleDialog.r -o "::HyperXExample Stack" -append
  33. *
  34.  
  35.             CASE    OBJ
  36.             INCLUDE    'Traps.a'
  37.             INCLUDE    'Quickdraw.a'
  38.             INCLUDE 'Resources.a'
  39.             INCLUDE    'HyperXCmd.a'
  40.         
  41.         
  42. okButton          EQU         1                         ; OK button
  43.  
  44. ;            PROCEDURE EntryPoint(paramPtr: XCmdPtr);
  45. ENTRYPOINT    MAIN    EXPORT        ; build script uses ENTRYPOINT as main segment name.
  46.  
  47. dialogID    EQU        10002
  48. iDefOKRing    EQU        okButton+1        ; user item number for default button circle.
  49. gap            EQU        -4
  50.  
  51. ; Local storage will be on A6 stack frame, which always counts backwards,
  52. ;    so values negative.
  53.  
  54. LOCALS        EQU        0
  55. myDialogPtr    EQU        LOCALS-4             ;DialogPtr
  56. savePort    EQU        myDialogPtr-4        ;GrafPtr
  57. itemHit        EQU        savePort-2            ;INTEGER
  58. tempType    EQU        itemHit-2            ;INTEGER
  59. tempHandle    EQU        tempType-4            ;Handle
  60. ctlRect        EQU        tempHandle-8        ;Rect
  61. displayStr    EQU        ctlRect-256            ;Str255
  62.  
  63. LOCALSIZE    EQU        displayStr
  64.  
  65.             IMPORT    DrawOKDefault
  66.             IMPORT    PASTOZERO
  67.             IMPORT    ZEROTOPAS
  68.             IMPORT    SENDHCMESSAGE
  69.     
  70.             WITH    XCmdBlock
  71.             STRING    ASIS
  72.     
  73.         ; Set up the stack frame for local variables.
  74.         
  75.         LINK    A6,#LOCALSIZE
  76.         MOVEM.L    A3/A4,-(SP)
  77.         
  78.         ; Get pointer to XCmdBlock
  79.         
  80.         MOVE.L    8(A6),A4            ; get paramPtr
  81.         CLR.L    returnValue(A4)        ; initialize return to empty
  82.     
  83.     
  84.         ; The heart and soul of the XFCN goes here.
  85. ;        PROCEDURE LittleDialog(paramPtr: XCmdPtr);
  86.  
  87.         ; Save the current port; good defensive programming practice.
  88.         ;    NOTE: this port will always be the card window's grafport.
  89.         
  90.         PEA        savePort(A6)
  91.         _GetPort
  92.         
  93.         ; It is always a good idea to check for the correct number of parameters.
  94.         ; Another nice idea is to return the proper syntax of the call if it has
  95.         ;    been called improperly.
  96.         
  97.         MOVE.W    paramCount(A4),D0
  98.         CMP.W    #1,D0                ; only XFCN parameter is input string
  99.         BNE        ErrAbort1
  100.  
  101.         ; This routine will use DLOG and DITL resources that must be available.
  102.         ;    Since XCMDs may be moved by ResEdit without knowledge of those resources,
  103.         ;    we must check for their availability. To suggest that our DLOG, DITL, and
  104.         ;    XCMD all belong together, we have numbered them all the same: 10002.
  105.         
  106.         CLR.L    -(SP)
  107.         MOVE.L    #'DLOG',-(SP)
  108.         MOVE.W    #dialogID,-(SP)
  109.         _GetResource
  110.         MOVE.L    (SP)+,D0
  111.         BEQ        ErrAbort2
  112.  
  113.         CLR.L    -(SP)
  114.         MOVE.L    #'DITL',-(SP)
  115.         MOVE.W    #dialogID,-(SP)
  116.         _GetResource
  117.         MOVE.L    (SP)+,D0
  118.         BEQ        ErrAbort2
  119.  
  120.         ; Use a HyperCard callback routine to convert the null-terminated input 
  121.         ;    string to a Str255 and transfer a copy to displayStr
  122.         
  123.         MOVE.L    A4,-(SP)            ; paramPtr
  124.         MOVE.L    params(A4),A0        ; paramPtr^.params[1]
  125.         MOVE.L    (A0),-(SP)            ; Dereference handle to be a ptr.
  126.         PEA        displayStr(A6)        ; Str255 VAR address
  127.         JSR        ZEROTOPAS            ; Convert input string to Str255.
  128.         
  129.         PEA        displayStr(A6)        ; Now contains Str255 we wish displayed.
  130.         PEA        NullStr
  131.         PEA        NullStr
  132.         PEA        NullStr
  133.         _ParamText
  134.  
  135.         CLR.L    -(SP)
  136.         MOVE.W    #dialogID,-(SP)
  137.         CLR.L    -(SP)
  138.         MOVE.L    #-1,-(SP)
  139.         _GetNewDialog                ; Get pointer to our dialog.
  140.         MOVE.L    (SP)+,D0
  141.         BEQ        ErrAbort3
  142.         MOVE.L    D0,A3
  143.  
  144.         ; Move our default-OK-button userItem to around the OK button, and set its
  145.         ;    item handle to be a pointer to our other drawing procedure.
  146.  
  147.         MOVE.L    A3,-(SP)            ; myDialogPtr
  148.         MOVE.W    #okButton,-(SP)        ; theItem
  149.         PEA        tempType(A6)
  150.         PEA        tempHandle(A6)
  151.         PEA        ctlRect(A6)
  152.         _GetDialogItem                ; Get rect of OK button.
  153.  
  154.         PEA        ctlRect(A6)
  155. ;        MOVE.W    #gap,-(SP)            ; dh
  156. ;        MOVE.W    #gap,-(SP)            ; dv
  157.         MOVE.L    #$FFFCFFFC,-(SP)
  158.         _InsetRect                    ; Make a larger rect surrounding the OK button.
  159.  
  160.         ; Set the same old type, our procptr, and the new box.
  161.         
  162.         MOVE.L    A3,-(SP)            ; myDialogPtr
  163.         MOVE.W    #iDefOKRing,-(SP)    ; the userItem number
  164.         MOVE.W    #userItem+itemDisable,-(SP)    ;itemType
  165.         PEA        DrawOKDefault        ; ProcPtr to draw user item.
  166.         PEA        ctlRect(A6)
  167.         _SetDialogItem                ; Get rect of OK button.
  168.  
  169.         ; Make the dialog window visible.
  170.         
  171.         MOVE.L    A3,-(SP)
  172.         _ShowWindow
  173.             
  174.         ; Set the cursor to the arrow for use with the dialog box.
  175.             
  176.         _InitCursor
  177.         
  178.         ; Make the modal dialog handle events.
  179.         
  180.         MOVE.L    #$0000FFFF,D0        ; $FFFF = everyEvent; $0000 = stopMask
  181.         _FlushEvents                ; Flush all events to be safe.
  182.  
  183. StayModal
  184.         CLR.L    -(SP)                ; No filter proc.
  185.         PEA        itemHit(A6)
  186.         _ModalDialog
  187.         MOVEQ    #okButton,D0
  188.         CMP.W    itemHit(A6),D0
  189.         BNE.S    StayModal
  190.  
  191.         ; Get rid of dialog and dialog handle.
  192.  
  193.         MOVE.L    A3,-(SP)
  194.         _DisposeDialog
  195.  
  196.         ; Let HyperCard set the cursor to a known state, so the next "idle"
  197.         ;    message after the dialog goes away will reset it to the correct
  198.         ;    cursor. (HyperCard doesn't know we did an InitCursor.
  199.             
  200.         MOVE.L    A4,-(SP)            ; paramPtr
  201.         PEA        HCCrsrMsg            ; Null terminated string.
  202.         JSR        SENDHCMESSAGE
  203.  
  204. Done
  205.         PEA        savePort(A6)        ; Restore the port.
  206.         _SetPort
  207.                                     ; Return to HyperCard.
  208.         MOVEM.L    (SP)+,A3/A4
  209.         UNLK    A6
  210.         MOVE.L    (SP)+,A0            ; return address
  211.         ADDQ.L    #4,SP                ; clean off input parameter
  212.         JMP        (A0)
  213.  
  214.         ; Error messages are loaded into A0 and then passed to the
  215.         ; ErrAbort procedure to be returned to HyperCard.
  216.  
  217. ErrAbort1
  218.         LEA        ParamErrStr,A0
  219.         BRA.S    ErrAbort
  220. ErrAbort2
  221.         LEA        RsrcErrStr,A0
  222.         BRA.S    ErrAbort
  223. ErrAbort3
  224.         LEA        MemErrStr,A0
  225. ;        BRA.S    ErrAbort
  226.  
  227. ErrAbort
  228.         ; PasToZero converts a pascal string to a null-terminated one AND
  229.         ; stores it away for us. Giving us a handle to pass back to HyperCard.
  230.  
  231.         CLR.L    -(SP)                ; Room for handle returned
  232.         MOVE.L    A4,-(SP)            ; paramPtr
  233.         MOVE.L    A0,-(SP)            ; enter with errMsg in A0
  234.         JSR        PASTOZERO            ; call routine in Interface.o
  235.         MOVE.L    (SP)+,returnValue(A4)
  236.         BRA.S    Done
  237.  
  238.             ENDWITH                    ; XCmdBlock
  239.  
  240.  
  241.             STRING    PASCAL
  242.         
  243. ParamErrStr
  244.         DC.B    'Correct usage is: "LittleDialog fieldName"'
  245. RsrcErrStr
  246.         DC.B    'XCMD requires resources: DLOG 10002 & DITL 10002.'
  247. MemErrStr
  248.         DC.B    'Not enough memory for dialog.'
  249. NullStr
  250.         DC.B    ''
  251. HCCrsrMsg
  252.         DC.B    'set cursor to 4'    ; 4 = the watch cursor
  253.  
  254.             ENDMAIN
  255.  
  256.  
  257.  
  258. ;PROCEDURE DrawOKDefault(theDialog: DialogPtr; theItem: INTEGER);
  259. DrawOKDefault    PROC    ENTRY
  260.  
  261. paramSize    EQU        6            ; pointer & integer
  262. curveRad    EQU        16
  263.  
  264. ; Local storage will be on A6 stack frame, this example uses a RECORD w/
  265. ; DECR to get offsets negative.
  266.  
  267. StackFrame    RECORD    0,DECR        ; build a stack frame record
  268. savePen        DS        PenState    ; PenState
  269. tempType    DS.W    1            ; INTEGER
  270. tempHandle     DS.L    1            ; Handle
  271. tempRect    DS.W    4            ; Rect
  272. localSize    EQU     *
  273.             ENDR
  274.  
  275.             WITH    StackFrame
  276.         
  277.         LINK    A6,#localSize        ; allocate our local stack frame
  278.  
  279.         PEA        savePen(A6)
  280.         _GetPenState                ; Save the old pen state.
  281.         
  282.         ; Draw an outline around our default button.
  283.         
  284.         MOVE.L    10(A6),-(SP)        ; theDialog
  285.         MOVE.W    8(A6),-(SP)            ; theItem
  286.         PEA        tempType(A6)
  287.         PEA        tempHandle(A6)
  288.         PEA        tempRect(A6)
  289.         _GetDialogItem                ; Get user item rect
  290.  
  291. ;        MOVE.W    #$0003,-(SP)        ; width
  292. ;        MOVE.W    #$0003,-(SP)        ; height
  293.         MOVE.L    #$00030003,-(SP)
  294.         _PenSize                    ; Make the pen fatter.
  295.         
  296.         PEA        tempRect(A6)
  297. ;        MOVE.W    #curveRad,-(SP)        ; ovalWidth
  298. ;        MOVE.W    #curveRad,-(SP)        ; ovalHeight
  299.         MOVE.L    #$00100010,-(SP)
  300.         _FrameRoundRect
  301.  
  302.         PEA        savePen(A6)
  303.         _SetPenState                ; Restore the old pen state.
  304.  
  305.         ; Exit the procedure.
  306.  
  307.         UNLK    A6                    ; destroy the link
  308.         MOVEA.L    (SP)+,A0            ; pull off the return address
  309.         ADDQ.L    #paramSize,SP        ; strip all of the caller's parameters
  310.         JMP    (A0)                    ; return to the caller
  311.  
  312.             ENDWITH                    ; StackFrame
  313.             ENDP
  314.  
  315.  
  316.             END
  317.